package dao

import (
	"github.com/go-xorm/xorm"
	"log"
	"lottery_code/comm"
	"lottery_code/models"
)

type GiftDao struct {
	engine *xorm.Engine
}

func NewGiftDao(engine *xorm.Engine) *GiftDao{
	return &GiftDao{
		engine:engine,
	}
}

func (d *GiftDao)Get(id int) (*models.LtGift,error){
	data:=&models.LtGift{Id: id}
	ok,err:=d.engine.Get(data)
	if !ok||err!=nil{
		data.Id=0
	}
	return data,err
}

func (d *GiftDao)Getall()[]models.LtGift {
	datalist:=make([]models.LtGift,0)
	err:=d.engine.
		Asc("sys_status").
		Asc("displayorder").
		Find(&datalist)
	if err!=nil{
		log.Println("gift_dao.GetAll err=",err)
	}
	return datalist
}

//自己写的
func (d *GiftDao) CountAll() int64{
	gift:=new(models.LtGift)
	num,err:=d.engine.Count(gift)
	if err!=nil{
		log.Println("gitf_dao.CountAll err=",err)
		return 0
	}
	return num
}

func (d *GiftDao)Delete(id int)error{
	data:=&models.LtGift{Id: id,SysStatus: 1}
	_,err:=d.engine.Id(data.Id).Update(data)
	return err
}

func (d *GiftDao)Update(data *models.LtGift, columns []string) error{
	_,err:=d.engine.Id(data.Id).MustCols(columns...).Update(data)
	return err
}

func (d *GiftDao)Create (data *models.LtGift)error{
	_,err:=d.engine.Insert(data)
	return err
}

// 获取到当前可以获取的奖品列表
// 有奖品限定，状态正常，时间期间内
// gtype倒序， displayorder正序
func (d *GiftDao) GetAllUse() []models.LtGift {
	now := comm.NowUnix()
	datalist := make([]models.LtGift, 0)
	err := d.engine.
		Cols("id", "title", "prize_num", "left_num", "prize_code",
			"prize_time", "img", "displayorder", "gtype", "gdata").
		Desc("gtype").
		Asc("displayorder").
		Where("prize_num>=?", 0). // 有限定的奖品
		Where("sys_status=?", 0). // 有效的奖品
		Where("time_begin<=?", now).   // 时间期内
		Where("time_end>=?", now).     // 时间期内
		Find(&datalist)
	if err != nil {
		return datalist
	} else {
		return datalist
	}
}

func (d *GiftDao)DecrLeftNum(id,num int)(int64,error){
	r,err:=d.engine.ID(id).
		Decr("left_num",num).
		Where("left_num>=?",num).
		Update(&models.LtGift{Id:id})
	return r,err
}

func (d *GiftDao)IncrLeftNum(id,num int)(int64,error){
	r,err:=d.engine.ID(id).
		Incr("left_num",num).
		Update(&models.LtGift{Id:id})
	return r,err
}

